<template>
  <a-dropdown v-if="currentUser && currentUser.name" placement="bottomRight">
    <span class="ant-pro-account-avatar">
      <a-avatar size="small" src="https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png" class="antd-pro-global-header-index-avatar" />
      <span>{{ currentUser.name }}</span>
    </span>
    <template v-slot:overlay>
      <a-menu class="ant-pro-drop-down menu" :selected-keys="[]">
        <!-- <a-menu-item v-if="menu" key="center" @click="handleToCenter">
          <a-icon type="user" />
          {{ $t('menu.account.center') }}
        </a-menu-item> -->
        <a-menu-item v-if="menu" key="settings" @click="handleToSettings">
          <a-icon type="setting" />
          修改密码
          <addModal
            ref="addModal"
            :visible="addVisible"
            :loading="confirmLoading"
            :model="mdl"
            @cancel="handleCancel"
            @ok="handleAddOk"
          />
        </a-menu-item>
        <a-menu-divider v-if="menu" />
        <a-menu-item key="logout" @click="handleLogout">
          <a-icon type="logout" />
          {{ $t('menu.account.logout') }}
        </a-menu-item>
      </a-menu>
    </template>
  </a-dropdown>
  <span v-else>
    <a-spin size="small" :style="{ marginLeft: 8, marginRight: 8 }" />
  </span>
</template>

<script>
import { Modal } from 'ant-design-vue'
import { changePassword } from '@/api/login'
import addModal from './addModal'
import storage from 'store'
import { ACCESS_TOKEN } from '@/store/mutation-types'

export default {
  name: 'AvatarDropdown',
  components: {
    addModal
  },
  props: {
    currentUser: {
      type: Object,
      default: () => null
    },
    menu: {
      type: Boolean,
      default: true
    }
  },
  data () {
    return {
      addVisible:false,
      confirmLoading: false,
      mdl: null,
    }
  },
  methods: {
    handleCancel () {
      this.addVisible = false
       // 清理表单数据
      const form = this.$refs.addModal.form
      form.resetFields() 
    },
    handleAddOk(){
      const form = this.$refs.addModal.form
      this.confirmLoading = true
      let _self = this
      form.validateFields((errors, values) => {
        if (!errors) {
          changePassword(values).then((res)=>{
            if(res.data.status == 200){
              _self.$message.success(`修改成功`)
              _self.addVisible = false
              // 重置表单数据
              form.resetFields()
              setTimeout(() => {
                storage.set(ACCESS_TOKEN,'')
                window.location.reload()
              }, 1000);

            }else{
              _self.$message.error(res.data.msg)
            }
            _self.confirmLoading = false
          })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleToCenter () {
      this.$router.push({ path: '/account/center' })
    },
    handleToSettings () {
      // this.$router.push({ path: '/account/settings' })
      this.addVisible = true
    },
    handleLogout (e) {
      Modal.confirm({
        title: this.$t('layouts.usermenu.dialog.title'),
        content: this.$t('layouts.usermenu.dialog.content'),
        onOk: () => {
          // return new Promise((resolve, reject) => {
          //   setTimeout(Math.random() > 0.5 ? resolve : reject, 1500)
          // }).catch(() => console.log('Oops errors!'))
          return this.$store.dispatch('Logout').then(() => {
            this.$router.push({ name: 'login' })
          })
        },
        onCancel () {}
      })
    }
  }
}
</script>

<style lang="less" scoped>
.ant-pro-drop-down {
  :deep(.action) {
    margin-right: 8px;
  }
  :deep(.ant-dropdown-menu-item) {
    min-width: 160px;
  }
}
</style>
